/*
 * @Author: your name
 * @Date: 2021-03-19 17:29:26
 * @LastEditTime: 2021-03-19 18:10:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \testcode\array.js
 */



Array.prototype.flat = function flat(depth = 1) {
    let newArr = []
    if (!depth) {
        return this
    }
    for (let item of this) {
        if (Object.prototype.toString.call(item) !== '[object Array]') {
            newArr.push(item)
        } else {
            for (let i of item.flat(depth - 1)) {
                newArr.push(i)
            }
        }
    }
    return newArr
}

let arr = [1, [2, [3]]]
console.log('arr.flat() = ', arr.flat(2));